package com.markyao.service;

import com.markyao.dao.mapper.CartMapper;
import com.markyao.dao.pojo.Cart;
import com.markyao.vo.CartVo;
import com.markyao.vo.Result;
import com.markyao.vo.parmas.CartParmas;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CartService {

    @Autowired
    CartMapper cartMapper;
    public void insertCart(CartParmas cartParmas) {
        cartMapper.insertCart(cartParmas);
    }

    public Result findByUid(Integer uid) {
        List<Integer> cids = cartMapper.findByUid(uid);
        if(cids.isEmpty())
        {
           return new Result(false,"该用户购物车为空",000,null);
        }
        List<CartVo> cartVos = cartMapper.findByCids(cids);
        return new Result(true,"查询成功",111,cartVos);
    }

    public Result add(Integer cid) {
        int num=cartMapper.findNumByCid(cid);

        cartMapper.addNum(cid,++num);
        return new Result(true,"增加数量成功",111,num);
    }

    public Result sub(Integer cid) {
        int num=cartMapper.findNumByCid(cid);
        if(num<=0){
            return new Result(false,"不能再减啦",000,num);
        }
        cartMapper.addNum(cid,--num);
        return new Result(true,"减少数量成功",111,num);
    }

    public Result findByCids(Integer[] cids) {
        List<Integer>integerList=new ArrayList<>();
        for (Integer cid : cids) {
            integerList.add(cid);
        }
        List<CartVo>cartVos=cartMapper.findByCids(integerList);
        return new Result(true,"查询成功",000,cartVos);
    }
}
